/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.excesys.exsecs.om.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.excesys.exsecs.om.dto.KnowledgeDto;
import com.excesys.exsecs.om.entity.OmOKnowledge;
import com.excesys.exsecs.om.entity.OmOKnowledgeFile;
import com.excesys.exsecs.om.mapper.OmOKnowledgeFileMapper;
import com.excesys.exsecs.om.mapper.OmOKnowledgeMapper;
import com.excesys.exsecs.om.service.OmOKnowledgeService;import com.excesys.exsecs.om.vo.KnowledgeVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

/**
 * 运维管理知识表
 *
 * @author weiyj
 * @date 2023-02-24 13:53:35
 */
@Service
@RequiredArgsConstructor
public class OmOKnowledgeServiceImpl extends ServiceImpl<OmOKnowledgeMapper, OmOKnowledge>
		implements OmOKnowledgeService {

	private final OmOKnowledgeFileMapper omOKnowledgeFileMapper;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean saveOmOKnowledge(KnowledgeDto knowledgeDto) {
		OmOKnowledge omOKnowledge = new OmOKnowledge();
		BeanUtils.copyProperties(knowledgeDto, omOKnowledge);
		baseMapper.insert(omOKnowledge);
		Optional.ofNullable(knowledgeDto.getFileList()).ifPresent(fileLists -> {
			fileLists.stream().map(omOKnowledgeFile -> {
				omOKnowledgeFile.setKnwlId(omOKnowledge.getKwId());
				return omOKnowledgeFile;
			}).forEach(omOKnowledgeFileMapper::insert);
		});
		return Boolean.TRUE;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean updateOmOKnowledge(KnowledgeDto knowledgeDto) {
		OmOKnowledge omOKnowledge = new OmOKnowledge();
		BeanUtils.copyProperties(knowledgeDto, omOKnowledge);
		this.updateById(omOKnowledge);
		omOKnowledgeFileMapper.delete(
				Wrappers.<OmOKnowledgeFile>lambdaQuery().eq(OmOKnowledgeFile::getKnwlId, knowledgeDto.getKwId()));
		// 保存用户岗位信息
		Optional.ofNullable(knowledgeDto.getFileList()).ifPresent(fileLists -> {
			fileLists.stream().map(omOKnowledgeFile -> {
				omOKnowledgeFile.setKnwlId(omOKnowledge.getKwId());
				return omOKnowledgeFile;
			}).forEach(omOKnowledgeFileMapper::insert);
		});
		return Boolean.TRUE;
	}

	@Override
	public IPage<KnowledgeVo> getKnowledgeWithRelPage(Page page, OmOKnowledge omOKnowledge) {
		return baseMapper.getKnowledgeWithRelPage(page, omOKnowledge);
	}

}
